$(function () {
    $("#employee_datagrid").datagrid({
        fit:true,
        url:'/employee/list',
        rownumbers:true,
        singleSelect:true,
        pagination:true,
        checkOnSelect:true,
        fitColumns:true,
        toolbar:employee_datagrid_Btn,
        columns:[
            [
            {field:'sn',title:'员工编号',align:'center',width:10},
            {field:'name',title:'员工名称',align:'center',width:10},
            {field:'password',title:'员工密码',align:'center',width:10},
            {field:'email',title:'员工邮箱',align:'center',width:10},
            {field:'age',title:'员工年龄',align:'center',width:10},
            {field:'admin',title:'管理员',align:'center',width:10,formatter:stateFormatter},
            {field:'dept',title:'所属部门',align:'center',width:10,formatter:deptFormatter}
            ]
        ]
    })
    $("#employee_dialog").dialog({
        title:'新增员工',
        width:300,
        height:450,
        buttons:'#employee_dialog_bt',
        closed: true
    })
    $("#searchBtn").textbox({
        onClickButton:function () {
           var keyword =  $("#searchBtn").val();
            $("#employee_datagrid").datagrid("load",{
                keyword:keyword
            })
        }
    })
})
function stateFormatter(value){
    if(value ==0){
        return "<font color='#adff2f'>是</font>"
    }else{
        return "<font color='red'>否</font>"
    }
}

function employee_add(){
    $("#employee_dialog").dialog("open")
    $("#employee_form").form("clear")
}
function deptFormatter(value) {
    return value.name;
}
//数据编辑===================================================================
function employee_edit() {
    var rowData = $("#employee_datagrid").datagrid("getSelected");
    if(rowData){
        $("#employee_dialog").dialog("open");
        //数据回显
        if(rowData.dept){
            rowData["dept.id"] = rowData.dept.id;
        }
        $.get("role/queryRoleIdByEmployeeId?employeeId="+rowData.id,function (data) {
             roleId.combobox("setValues",data);
        },"json")
        $("#employee_form").form("load",rowData);
    }else{
        $.messager.alert("温馨提示","请选择需要编辑的数据","warning");
    }
}
function employee_cancel() {
    //进行取消窗口页面
    $("#employee_dialog").dialog("close")
}
function employee_reload() {
    //进行刷新表单
    $("#employee_datagrid").datagrid("reload");
}

//添加==================================================================
function employee_save() {
     var url;
        var idVal = $("[name=id]").val();
    if(idVal){
        //进行判断
        url = "/employee/update"
    }else{
        url ="/employee/save"
    }
    $("#employee_form").form("submit",{
     url:url,
        onSubmit:function (param) {
         var ids =   $("#roleId").combobox("getValues");
            for(var i =0 ;i<ids.length; i++ ){
                        param["roles["+i+"].id"] = ids[i];
            }
            return true;
        },
        success:function (data) {
           data=  $.parseJSON(data);
           if(data.success){
               $.messager.alert("温馨提示",data.msg,"info",function () {
                   //刷新数据表格
                   $("#employee_datagrid").datagrid("reload");
                   //关闭对话框
                   $("#employee_dialog").dialog("close")
               });
               }
               else {
               $.messager.alert("温馨提示",data.msg,"error")
           }
        }
    });
}
//删除==================================================================
function employee_remove(){
    var rowData = $("#employee_datagrid").datagrid("getSelected");
    if(rowData){
        $.messager.confirm('温馨提示','您确认想要删除记录吗？',function(yes){
            if(yes){
                $.post("/employee/delete?id="+rowData.id,function (data) {
                    if(data.success){
                        $.messager.alert("温馨提示",data.msg,"info",function () {
                            $("#employee_datagrid").datagrid("reload");
                        });
                    }else {
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                },"json");
            }
        });
    }else{
        $.messager.alert("温馨提示","请选择需要删除的数据","warning");
    }
}